package com.uzai.console.service.alarm.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.common.entity.BaseEntity;
import com.uzai.common.enums.DeviceTypeEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.ResponseMsg;
import com.uzai.console.dto.alarm.alarmdevice.AlarmDeviceQuery;
import com.uzai.console.dto.wechatprivate.devicewechatinfo.DeviceWechatAlarmRuleIdDto;
import com.uzai.console.dto.wechatprivate.devicewechatinfo.DeviceWechatAlarmRuleIdListDto;
import com.uzai.console.dto.wechatprivate.devicewechatinfo.DeviceWechatInfoQuery;
import com.uzai.console.entity.*;
import com.uzai.console.mapper.AlarmRuleMapper;
import com.uzai.console.mapper.DeviceWechatInfoMapper;
import com.uzai.console.mapper.DeviceWxworkInfoMapper;
import com.uzai.console.service.alarm.AlarmDeviceService;
import com.uzai.console.service.wechatprivate.DeviceWechatInfoService;
import com.uzai.console.vo.alarm.alarmrule.AlarmRuleInfoVo;
import com.uzai.console.vo.wechatprivate.devicewechatinfo.DeviceWechatInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 设备监控
 * @author liuqi
 * @date 2021年08月13日 11:34
 */
@Service
public class AlarmDeviceServiceImpl implements AlarmDeviceService {

    Logger logger = LoggerFactory.getLogger(AlarmDeviceServiceImpl.class);

    @Autowired
    private DeviceWechatInfoMapper deviceWechatInfoMapper;
    @Autowired
    private AlarmRuleMapper alarmRuleMapper;
    @Autowired
    private DeviceWechatInfoService deviceWechatInfoService;
    @Autowired
    private DeviceWxworkInfoMapper deviceWxworkInfoMapper;

    /**
     * 查询设备监控列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param alarmDeviceQuery
     * @return null
     */
    public Page<DeviceWechatInfoVo> findAlarmDeviceInfoList(AlarmDeviceQuery alarmDeviceQuery){
        //转化成个人微信查询参数对象
        DeviceWechatInfoQuery deviceWechatInfoQuery = new DeviceWechatInfoQuery();
        BeanUtils.copyProperties(alarmDeviceQuery, deviceWechatInfoQuery);
        deviceWechatInfoQuery.setMerId(alarmDeviceQuery.getMerId());
        deviceWechatInfoQuery.setGroupId(alarmDeviceQuery.getGroupId());
        Page<DeviceWechatInfoVo> page = deviceWechatInfoService.findDeviceWechatInfoList(deviceWechatInfoQuery);
        //将分页信息复制到返回到页面的分页MO当中
        return page;
    }

    /**
     * 设置设备监控的报警规则
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param deviceWechatAlarmRuleIdDto
     * @return null
     */
    public void setAlarmDeviceAlarmRule(DeviceWechatAlarmRuleIdDto deviceWechatAlarmRuleIdDto){

        if(deviceWechatAlarmRuleIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入机器人id");
        }

        if(StringUtils.isBlank(deviceWechatAlarmRuleIdDto.getDeviceType())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入设备类型");
        }

        //修改个人微信机器人报警规则
        if(deviceWechatAlarmRuleIdDto.getDeviceType().equals(DeviceTypeEnum.DT_PC_WECHAT.getKey())){
            deviceWechatInfoMapper.updateAlarmRuleById(deviceWechatAlarmRuleIdDto);
            //企业微信机器人报警规则
        } else if(deviceWechatAlarmRuleIdDto.getDeviceType().equals(DeviceTypeEnum.DT_WORK_WECHAT.getKey())){
            deviceWxworkInfoMapper.updateAlarmRuleById(deviceWechatAlarmRuleIdDto);
        }
    }

    /**
     * 批量修改设备监控的报警规则
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param deviceWechatAlarmRuleIdListDto
     * @return null
     */
    @Transactional
    public void batchSetAlarmDeviceAlarmRule(DeviceWechatAlarmRuleIdListDto deviceWechatAlarmRuleIdListDto){

        if(StringUtils.isBlank(deviceWechatAlarmRuleIdListDto.getDeviceType())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入设备类型");
        }

        List<Long> idList = deviceWechatAlarmRuleIdListDto.getIdList();

        if(idList == null || idList.size() == 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        //修改个人微信机器人报警规则
        if(deviceWechatAlarmRuleIdListDto.getDeviceType().equals(DeviceTypeEnum.DT_PC_WECHAT.getKey())){
            for(Long id : idList){
                DeviceWechatAlarmRuleIdDto deviceWechatAlarmRuleIdDto = new DeviceWechatAlarmRuleIdDto();
                BeanUtils.copyProperties(deviceWechatAlarmRuleIdListDto, deviceWechatAlarmRuleIdDto);
                deviceWechatAlarmRuleIdDto.setMerId(deviceWechatAlarmRuleIdListDto.getMerId());
                deviceWechatAlarmRuleIdDto.setId(id);
                deviceWechatAlarmRuleIdDto.setAlarmRuleId(deviceWechatAlarmRuleIdListDto.getAlarmRuleId());
                deviceWechatInfoMapper.updateAlarmRuleById(deviceWechatAlarmRuleIdDto);
            }
        } else if(deviceWechatAlarmRuleIdListDto.getDeviceType().equals(DeviceTypeEnum.DT_WORK_WECHAT.getKey())){
            for(Long id : idList){
                DeviceWechatAlarmRuleIdDto deviceWechatAlarmRuleIdDto = new DeviceWechatAlarmRuleIdDto();
                BeanUtils.copyProperties(deviceWechatAlarmRuleIdListDto, deviceWechatAlarmRuleIdDto);
                deviceWechatAlarmRuleIdDto.setMerId(deviceWechatAlarmRuleIdListDto.getMerId());
                deviceWechatAlarmRuleIdDto.setId(id);
                deviceWechatAlarmRuleIdDto.setAlarmRuleId(deviceWechatAlarmRuleIdListDto.getAlarmRuleId());
                deviceWxworkInfoMapper.updateAlarmRuleById(deviceWechatAlarmRuleIdDto);
            }
        }

    }

}
